"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2022/5/17 21:09
@Software: PyCharm
@File    : ui_setting.py
@Remark  : 
"""
from typing import Union

from PySide2.QtCore import Qt, Slot, Signal
from PySide2.QtGui import QStandardItemModel, QStandardItem
from PySide2.QtWidgets import QWidget
from pyqtgraph.parametertree import Parameter, ParameterTree

from ui_component.ui_main.ui_designer.ui_setting import Ui_Form
from ui_component.ui_variable import UiGlobalVariable


class SettingWidget(QWidget, Ui_Form):
    group_item_list = QStandardItemModel()
    da_group_item_list = QStandardItemModel()
    group_emit = Signal()
    jmp_signal = Signal()
    graph_signal = Signal()

    def __init__(self, parent=None):
        super(SettingWidget, self).__init__(parent)
        self.parent = parent
        self.setupUi(self)
        self.setWindowTitle("Setting Tool")
        self.listView.setModel(self.group_item_list)
        self.listView_2.setModel(self.da_group_item_list)
        self.init_listView()
        self.init_listView_2()
        self.jmp_t = ParameterTree()
        self.graph_t = ParameterTree()
        self.jmp_t.hide()
        self.graph_t.hide()
        self.verticalLayout.addWidget(self.jmp_t)
        self.verticalLayout.addWidget(self.graph_t)
        self.kwargs = {}
        self.init_tree_params(self.graph_t, UiGlobalVariable.GRAPH_PARAMS)

        self.init_kwargs(UiGlobalVariable.GRAPH_PARAMS)
        print(self.kwargs)
        self.radioButton_2.setChecked(Qt.Checked)
        self.on_radioButton_2_clicked()

        self.group_item_list.itemChanged.connect(self.checkbox_changed)
        self.da_group_item_list.itemChanged.connect(self.checkbox_changed)

    def init_tree_params(self, params_tree: ParameterTree, params: list):
        p = Parameter.create(name='params', type='group', children=params)
        p.sigTreeStateChanged.connect(self.change)
        params_tree.setParameters(p, showTop=False)
        self.init_kwargs(params)

    def init_kwargs(self, params: list):
        for each in params:  # type:dict
            if 'value' in each:
                self.kwargs[each['name']] = each['value']
            if 'children' in each:
                self.init_kwargs(each['children'])

    def change(self, param, changes):
        for param, change, data in changes:
            self.kwargs[param.name()] = data

    def init_listView(self):
        """
        初始化Group By,后面放在ini中维护
        :return:
        """
        self.group_item_list.clear()
        for index, each in enumerate(UiGlobalVariable.SUMMARY_GROUP):
            item = QStandardItem(each)
            if index == 0:
                item.setCheckState(Qt.Checked)
            else:
                item.setCheckState(Qt.Unchecked)
            item.setCheckable(True)
            self.group_item_list.appendRow(item)

    def init_listView_2(self):
        """
        初始化Group By,后面放在ini中维护
        :return:
        """
        self.da_group_item_list.clear()
        for index, each in enumerate(UiGlobalVariable.DATA_GROUP):
            item = QStandardItem(each)
            item.setCheckState(Qt.Unchecked)
            item.setCheckable(True)
            self.da_group_item_list.appendRow(item)

    def get_group_params(self) -> Union[list, None]:
        """
        获取Group By
        :return:
        """
        group_list = []
        for i in range(len(UiGlobalVariable.SUMMARY_GROUP)):
            temp = self.group_item_list.item(i)  # type:QStandardItem
            if temp.checkState() == Qt.Checked:
                group_list.append(temp.text())
        return group_list if group_list else None

    def get_da_group_params(self) -> Union[list, None]:
        """
        获取Group By
        :return:
        """
        group_list = []
        for i in range(len(UiGlobalVariable.DATA_GROUP)):
            temp = self.da_group_item_list.item(i)  # type:QStandardItem
            if temp.checkState() == Qt.Checked:
                group_list.append(temp.text())
        return group_list if group_list else None

    @Slot()
    def on_radioButton_2_clicked(self):
        self.jmp_t.hide()
        self.graph_t.show()
        self.graph_signal.emit()

    @Slot()
    def checkbox_changed(self):
        self.group_emit.emit()


if __name__ == '__main__':
    import sys
    from PySide2.QtWidgets import QApplication, QWidget

    app = QApplication(sys.argv)
    win = SettingWidget()
    win.show()
    sys.exit(app.exec_())
